package com.lkj.youmeng.mvp.activity;


import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.lkj.youmeng.R;
import com.lkj.youmeng.adapter.BannerAdapter;
import com.lkj.youmeng.base.BaseActivity;
import com.lkj.youmeng.data.BannerData;
import com.lkj.youmeng.mvp.contract.HomeContract;
import com.lkj.youmeng.mvp.fragment.ContentFragment;
import com.lkj.youmeng.base.BaseTitleView;
import com.lkj.youmeng.mvp.presenter.HomePresenter;
import com.lkj.youmeng.mvp.view.MergeImageView;

import java.util.ArrayList;

public class HomeActivity extends BaseActivity implements HomeContract.View {

    private BaseTitleView mTitleView;
    private ContentFragment mContentFragment;
    private ViewPager mViewPager;
    private LinearLayout mContent;
    private int previousSelectedPosition = 0;
    boolean isRunning_AD = false;   //焦点图
    private HomeContract.Present mPresent;
    private ArrayList<BannerData> mBannerList = new ArrayList<>();
    private int[] mImg_id = new int[]{
            R.id.pager_img1,
            R.id.pager_img2,
            R.id.pager_img3,
            R.id.pager_img4,
            R.id.pager_img5
    };
    private String[] mPicUrl = new String[]{
            "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2476261407,3935284840&fm=26&gp=0.jpg",
            "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=259884630,185584937&fm=26&gp=0.jpg",
            "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1062949334,1203696969&fm=26&gp=0.jpg",
            "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=2942915402,4281822602&fm=26&gp=0.jpg"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initTitle();
        setPresenter(new HomePresenter(this));
        initFragment();
        mPresent.getData();
    }

    private void initView() {
        mTitleView = findViewById(R.id.title);
        mViewPager = findViewById(R.id.home_viewpager);
        mContent = findViewById(R.id.home_content);
    }

    private void initTitle() {
//        mTitleView.setTitle("友萌");
    }

    private void initFragment() {
        setContainerId(R.id.fragment_content);
        mContentFragment = ContentFragment.newInstance(null);
//        mRecommendFragment = RecommendFragment.newInstance(null);
        addFragmentToStack(mContentFragment);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode){
            case KeyEvent.KEYCODE_BACK:
                finish();
                break;
        }
        return true;
    }

    @Override
    public void onSuccess() {
        for (int i = 0;i < mPicUrl.length;i++){
            BannerData data = new BannerData();
            data.setId(i);
            data.setPic(mPicUrl[i]);
            mBannerList.add(data);
        }
        updateViewPager();
    }

    private void updateViewPager() {
        final ArrayList<MergeImageView> mImageViewList = new ArrayList<>();
        for (int i = 0; i < mBannerList.size(); i++) {
            final MergeImageView imageView = new MergeImageView(this);
            imageView.setId(mImg_id[i]);
            imageView.setOnClickListener(new pagerOnClickListener(this));
            String url = mBannerList.get(i).getPic();
            if (this != null) {
                Glide.with(this).load(url).into(new SimpleTarget<Drawable>() {
                    @Override
                    public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                        imageView.setBackgroundDrawable(resource);
                    }
                });
            }
            mImageViewList.add(imageView);
            //引导图
            ImageView view = new ImageView(this);
            view.setImageResource(R.mipmap.icon_page);
            view.setAlpha(0.2f);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(20, 20);
            if (i != 0) {
                layoutParams.leftMargin = 10;
            }
            mContent.addView(view, layoutParams);
        }
        mContent.getChildAt(0).setAlpha(1f);
        previousSelectedPosition = 0;
        BannerAdapter adapter = new BannerAdapter(mImageViewList);
        mViewPager.setAdapter(adapter);
        mViewPager.setCurrentItem(0);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (mImageViewList.size() > 0) {
                    int newPosition = position % mImageViewList.size();
                    if (mContent.getChildCount() > 0) {
                        mContent.getChildAt(previousSelectedPosition).setAlpha(0.2f);
                        mContent.getChildAt(newPosition).setAlpha(1f);
                    }
                    previousSelectedPosition = newPosition;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        // 开启轮询
        new Thread() {
            public void run() {
                isRunning_AD = true;
                while (isRunning_AD) {
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //下一条
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            int currentItem = mViewPager.getCurrentItem();
                            if (mBannerList.size() > 1) {
                                mViewPager.setCurrentItem(mViewPager.getCurrentItem() + 1);
                            } else {
                                mViewPager.setCurrentItem(mViewPager.getCurrentItem());
                            }

                        }
                    });
                }
            }
        }.start();

    }

    @Override
    public void onFail(String msg) {

    }

    @Override
    public void setPresenter(HomeContract.Present presenter) {
        mPresent = presenter;
    }

    public class pagerOnClickListener implements View.OnClickListener {
        Activity activity;

        public pagerOnClickListener(Activity activity) {
            this.activity = activity;
        }

        @Override
        public void onClick(View v) {

            switch (v.getId()) {
                case R.id.pager_img1:
                    break;
                case R.id.pager_img2:
                    break;
                case R.id.pager_img3:
                    break;
                case R.id.pager_img4:
                    break;
                case R.id.pager_img5:
                    break;
            }
        }
    }

    @Override
    protected void onDestroy() {
        isRunning_AD = false;
        super.onDestroy();
    }
}
